package cn.xfdzx.android.apps.shop.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.FamliyItemBeen;
import cn.xfdzx.android.apps.shop.presenter.FamilyPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * Creator: zf.
 * Date: 2020/8/24.
 * Description:家庭购商品.
 */
public class FamilyGoodListAdapter extends BaseQuickAdapter<FamliyItemBeen.DataBean, BaseViewHolder> {
    //private AfterServiceGoodsListAdapter.OnItemOnClickListener onItemOnClickListener;
    private List<FamliyItemBeen.DataBean> dataBeans;//商品列表
    private int currentActiveState;
    RoundedCorners roundedCorners;
    RequestOptions options;
    private FamilyPresenter familyPresenter;

    public FamilyGoodListAdapter(@Nullable List<FamliyItemBeen.DataBean> data, FamilyPresenter familyPresenter) {
        super(R.layout.item_family_fang, data);
        this.dataBeans = data;
        this.familyPresenter = familyPresenter;

        roundedCorners = new RoundedCorners(20);
        options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
    }

    @Override
    public void setData(int index, @NonNull FamliyItemBeen.DataBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final FamliyItemBeen.DataBean date) {
        ImageView ivGood = viewHolder.getView(R.id.iv_good);
        TextView tvGoodTitle = viewHolder.getView(R.id.tv_good_title);
        TextView tvPrice = viewHolder.getView(R.id.tv_price);
        TextView tvPriceOld = viewHolder.getView(R.id.tv_price_old);
        TextView tvCarNum = viewHolder.getView(R.id.tv_car_num);
        ImageView ivAdd = viewHolder.getView(R.id.iv_add);

        tvCarNum.setText(date.getCart_num() + "");
        if (date.getCart_num() == 0) {
            tvCarNum.setVisibility(View.GONE);
        } else {
            tvCarNum.setVisibility(View.VISIBLE);
        }
        Glide.with(mContext).load(date.getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivGood);

        tvGoodTitle.setText(date.getTitle());
        if (Double.parseDouble(date.getDiscountprice()) == 0) {
            tvPrice.setText("¥" + date.getMarketprice());
            tvPriceOld.setVisibility(View.GONE);
        } else {
            tvPrice.setText("¥" +date.getDiscountprice());
            tvPriceOld.setText("¥" + date.getMarketprice());
            tvPriceOld.setVisibility(View.VISIBLE);
            tvPriceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        }
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EmptyUtil.isEmpty(APP_TOKEN)) {
                    Intent intent = new Intent(mContext, OneLoginMainActivity.class);
                    mContext.startActivity(intent);
                    return;
                }

                familyPresenter.addCar("0", "", date.getGoods_id() + "", "1", viewHolder.getLayoutPosition()-1, date.getCart_num() + 1);

            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ShopActivity.class);
                intent.putExtra("good_id", date.getGoods_id() + "");
                intent.putExtra("shop_id", date.getRole_id() + "");
              mContext. startActivity(intent);
            }
        });

    }

    public void clearDatas() {
        mData.clear();
        notifyDataSetChanged();
    }

    public void refreshDatas(List<FamliyItemBeen.DataBean> list, int currentActiveState) {
        this.dataBeans = list;
        this.currentActiveState = currentActiveState;
        super.replaceData(list);
    }

    public void addDatas(List<FamliyItemBeen.DataBean> list) {
        super.addData(list);
        //mData.addAll(list);
    }


    private FamilyGoodListAdapter.OnItemOnClickListener onItemOnClickListener;

    public void setOnListItemGoBuyListener(FamilyGoodListAdapter.OnItemOnClickListener onItemGoBuy) {
        this.onItemOnClickListener = onItemGoBuy;
    }

    public interface OnItemOnClickListener {
        void onItemBuyClick(String goodsId);
    }
}
